package com.om.common;



import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;



import com.om.base.AuthorizationCache;

import com.om.base.Utils;
import com.om.common.BaseAuthorizationUser;
import com.om.common.ConfigReader;
import com.om.common.GuidCreator;


public class YdAuthorizationCache extends AuthorizationCache {

	public void cacheLoginUser(BaseAuthorizationUser user
			,HttpServletRequest request
			, HttpServletResponse response ) {
		HttpSession session = null;
		
		String save2Cookie = "false";
		try {

			
			save2Cookie = ConfigReader.GetProptery("save_2_cookie");

			session = request.getSession();

			if (!"".equals(ConfigReader.GetProptery("login_user_key"))) {
				session.setAttribute(
						ConfigReader.GetProptery("login_user_key"), user);
			}

			if (!"".equals(ConfigReader.GetProptery("login_user_id_key"))) {
				session.setAttribute(
						ConfigReader.GetProptery("login_user_id_key"),
						user.getUserID());

				if (save2Cookie.equals("true")) {
					Utils.SetCookieValue(response,
							ConfigReader.GetProptery("login_user_id_key"),
							user.getUserID() + "");
				}

			}

			if (!"".equals(ConfigReader.GetProptery("login_user_name_key"))) {
				session.setAttribute(
						ConfigReader.GetProptery("login_user_name_key"),
						user.getUserName());

				if (save2Cookie.equals("true")) {
					Utils.SetCookieValue(response,
							ConfigReader.GetProptery("login_user_name_key"),
							URLEncoder.encode(user.getUserName()));
				}

			}

			if (!"".equals(ConfigReader.GetProptery("login_password_key"))) {
				session.setAttribute(
						ConfigReader.GetProptery("login_password_key"),
						user.getPassword());

				if (save2Cookie.equals("true")) {
					Utils.SetCookieValue(response,
							ConfigReader.GetProptery("login_password_key"),
							user.getPassword());

				}

			}

			if (!"".equals(ConfigReader.GetProptery("login_user_powers_key"))) {
				session.setAttribute(
						ConfigReader.GetProptery("login_user_powers_key"),
						user.getUserPowers());

				if (save2Cookie.equals("true")) {
					Utils.SetCookieValue(response,
							ConfigReader.GetProptery("login_user_powers_key"),
							user.getUserPowers());

				}

			}

			if (!ConfigReader.GetProptery("memche_server").equals("")) {
				GuidCreator myGUID = new GuidCreator();
				String memcacheId = myGUID.createNewGuid(GuidCreator.AfterMD5);

				Utils.SetCookieValue(response,
						ConfigReader.GetProptery("cookie_key"), memcacheId);

				

			}

		} catch (Exception ex) {
			ex.printStackTrace();
		}

	}

	@Override
	public BaseAuthorizationUser getLoginUser(HttpServletRequest request
			,HttpServletResponse response ) {
		BaseAuthorizationUser loginUser = null;

		try {

			HttpSession session = request.getSession();
			if (session.getAttribute(ConfigReader.GetProptery("login_user_key")) != null) {
				loginUser = (BaseAuthorizationUser) session
						.getAttribute(ConfigReader.GetProptery("login_user_key"));
			}

			
			
			

		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return loginUser;
	}

	@Override
	public BaseAuthorizationUser getLoginUser(Class AuthorizationUserClass,
			HttpServletRequest request,HttpServletResponse response) {
		BaseAuthorizationUser loginUser = null;

		try {
			loginUser = getLoginUser(request,response);
			
			if (loginUser == null && AuthorizationUserClass != null) {
				loginUser = this.reLoadLoginUser(AuthorizationUserClass, request, response);
				if(loginUser != null){
					//加载权限，角色等。。
					loginUser.afterLogin();
				}
			}
			if(loginUser!= null){
				cacheLoginUser(loginUser,request,response);
			}
			
			
			
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return loginUser;

	}
	@Override
	public BaseAuthorizationUser reLoadLoginUser(Class AuthorizationUserClass,HttpServletRequest request
			,HttpServletResponse response){
		BaseAuthorizationUser loginUser = null; 
		Object userId = this.getLoginUserID(request);
		if (userId != null && !"".equals(userId)) {

			try {
				loginUser = (BaseAuthorizationUser) Class.forName(AuthorizationUserClass.getName()).newInstance();
				loginUser.loadItem(userId);
			} catch (Exception ex) {
				// TODO Auto-generated catch block
				ex.printStackTrace();
			}
			
		}
		
		return loginUser;
	} 

	@Override
	public String getLoginName(HttpServletRequest request) {
		String userName = "";

		try {

			if (request.getSession().getAttribute(
					ConfigReader.GetProptery("login_user_name_key")) != null) {
				userName = request.getSession().getAttribute(
						ConfigReader.GetProptery("login_user_name_key"))
						+ "";
			} else if (Utils.GetCookieValue(request,
					ConfigReader.GetProptery("login_user_name_key")) != null) {
				userName = URLDecoder.decode(Utils.GetCookieValue(request,
						ConfigReader.GetProptery("login_user_name_key")));
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return userName;
	}

	@Override
	public String getLoginPassword(HttpServletRequest request) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object getLoginUserID(HttpServletRequest request) {
		String userId = "";
		try {

			if (request.getSession().getAttribute(
					ConfigReader.GetProptery("login_user_id_key")) != null) {
				userId = request.getSession().getAttribute(
						ConfigReader.GetProptery("login_user_id_key"))
						+ "";
			} else if ( Utils.GetCookieValue(request,
							ConfigReader.GetProptery("login_user_id_key")) != null) {
				userId = Utils.GetCookieValue(request,
						ConfigReader.GetProptery("login_user_id_key"));

			}

		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return userId;
	}

	@Override
	public String getLoginUserPowers(HttpServletRequest request) {
		String powers = "";

		try {

			if (request.getSession().getAttribute(
					ConfigReader.GetProptery("login_user_powers_key")) != null) {
				powers = request.getSession().getAttribute(
						ConfigReader.GetProptery("login_user_powers_key"))
						+ "";
			} else if (Utils.GetCookieValue(request,
					ConfigReader.GetProptery("login_user_powers_key")) != null) {
				powers = Utils.GetCookieValue(request,
						ConfigReader.GetProptery("login_user_powers_key"));

			} 

		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return powers;
	}

	@Override
	public void destroyLoginUser(HttpServletRequest request,HttpServletResponse response) {
		HttpSession session = null;
		
		try {
			
			session = request.getSession();

			if (!"".equals(ConfigReader.GetProptery("login_user_id_key"))) {
				if (!ConfigReader.GetProptery("memche_server").equals("")) {
					String memcacheId = Utils.GetCookieValue(request,
							ConfigReader.GetProptery("cookie_key"));
					

				}

				session.setAttribute(
						ConfigReader.GetProptery("login_user_id_key"), null);
				Utils.ClearCookie(request, response,
						ConfigReader.GetProptery("login_user_id_key"));

			}

			if (!"".equals(ConfigReader.GetProptery("login_user_key"))) {
				session.setAttribute(
						ConfigReader.GetProptery("login_user_key"), null);
			}

			if (!"".equals(ConfigReader.GetProptery("login_user_name_key"))) {
				session.setAttribute(
						ConfigReader.GetProptery("login_user_name_key"), null);

				Utils.ClearCookie(request, response,
						ConfigReader.GetProptery("login_user_name_key"));
			}

			if (!"".equals(ConfigReader.GetProptery("login_password_key"))) {

				session.setAttribute(
						ConfigReader.GetProptery("login_password_key"), null);

				Utils.ClearCookie(request, response,
						ConfigReader.GetProptery("login_password_key"));

			}

			if (!"".equals(ConfigReader.GetProptery("login_user_powers_key"))) {
				session.setAttribute(
						ConfigReader.GetProptery("login_user_powers_key"), null);

				Utils.ClearCookie(request, response,
						ConfigReader.GetProptery("login_user_powers_key"));
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		}

	}

	@Override
	public ArrayList<String> getPowerList(HttpServletRequest request,HttpServletResponse response ) {
		ArrayList<String> powerList = new ArrayList<String>();
		BaseAuthorizationUser user = this.getLoginUser(request,response);
		if (user != null) {
			powerList = user.getPowerList();
		}
		return powerList;

	}

	@Override
	public String getLoginUserRoles(HttpServletRequest request,HttpServletResponse response ) {
		String roles="";
		BaseAuthorizationUser user = this.getLoginUser(request,response);
		if (user != null) {
			roles = user.roles;
		}
		return roles;
	}

	

}
